﻿namespace NetworkFlowMonitor.Scripts.Service;

// Hubs/TrafficHub.cs
using Microsoft.AspNetCore.SignalR;

public class TrafficHub : Hub
{
    private static readonly List<string> _connectedClients = new();

    public override async Task OnConnectedAsync()
    {
        var connectionId = Context.ConnectionId;
        _connectedClients.Add(connectionId);
        
        await base.OnConnectedAsync();
        await Clients.Caller.SendAsync("ConnectionEstablished", connectionId);
    }

    public override async Task OnDisconnectedAsync(Exception? exception)
    {
        var connectionId = Context.ConnectionId;
        _connectedClients.Remove(connectionId);
        
        await base.OnDisconnectedAsync(exception);
    }

    public async Task SendTestUpdate()
    {
        var update = new TrafficUpdate
        {
            DeviceName = "Test Device",
            IpAddress = "192.168.1.100",
            MacAddress = "aa:bb:cc:dd:ee:ff",
            DownloadSpeed = 102400,
            UploadSpeed = 51200,
            TotalDownload = 1048576,
            TotalUpload = 524288,
            Timestamp = DateTime.UtcNow
        };

        await Clients.All.SendAsync("ReceiveTrafficUpdate", new[] { update });
    }
}